package com.shuoyue.ycgk.ui.mine.vip;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.VipPurchased;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface VipCheckContract {
    interface View extends BaseView {
        void isBuy(boolean isBuy);

        void setPurchased(List<VipPurchased> purchased);
    }

    class Presenter extends BasePresenter<View> {
        Model mo = new Model();

        public void checkVipStatue(int typeId) {
            apply(mo.checkVipStatue(typeId))
                    .subscribe(new ApiSubscriber<Optional<Integer>>(mView, this) {
                        @Override
                        public void onNext(Optional<Integer> tOptional) {
                            super.onNext(tOptional);
                            mView.isBuy(tOptional.getIncludeNull() != null && tOptional.getIncludeNull() == 0);
                        }
                    });
        }

        public void getVipPurchased( ) {
            apply(mo.getVipPurchased( ))
                    .subscribe(new ApiSubscriber<Optional<List<VipPurchased>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<VipPurchased>> tOptional) {
                            super.onNext(tOptional);
                            mView.setPurchased(tOptional.getIncludeNull());
                        }
                    });
        }

    }

    class Model {
        Observable<BaseResult<Integer>> checkVipStatue(int subjectTypeId) {
            return RetrofitClient.getInstance().getApi().checkVipStatue(subjectTypeId);
        }

        Observable<BaseResult<List<VipPurchased>>> getVipPurchased( ) {
            return RetrofitClient.getInstance().getApi().getVipPurchased( );
        }
    }
}
